package com.android.smsmessing.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

import com.android.smsmessing.R;
import com.android.smsmessing.adapter.PeoplesAdapter.ViewHolder;
import com.android.smsmessing.model.PeopleVO;
import com.android.smsmessing.model.SMSInboxVO;
import com.android.smsmessing.utils.DateUtils;
import com.android.smsmessing.utils.StringUtils;

public class SMSInboxAdapter extends BaseAdapter {
	private LayoutInflater mInflater = null;
	Date now = new Date();
	Date displayDate = new Date();
	HashMap<Integer,SMSInboxVO> keyMap = null;
	public SMSInboxAdapter(Context context){
		mInflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 重置数据
	 * @param map
	 * @param keyMap
	 */
	public void resertData(HashMap<Integer,SMSInboxVO> keyMap){
		this.keyMap = keyMap;
		notifyDataSetChanged();
	}
	public static class ViewHolder {
		TextView txtPerson;
		TextView txtTotal;
		TextView txtSummary;
		TextView txtTime;
		public SMSInboxVO smsInboxVO;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(keyMap != null){
			return keyMap.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_smsinbox_summary, null);
			holder.txtPerson = (TextView) convertView.findViewById(R.id.txtPerson);
			holder.txtTotal = (TextView) convertView.findViewById(R.id.txtTotal);
			holder.txtSummary = (TextView) convertView.findViewById(R.id.txtSummary);
			holder.txtTime = (TextView)convertView.findViewById(R.id.txtTime);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		SMSInboxVO smsInbox = keyMap.get(position);
		
		holder.txtPerson.setText(smsInbox.name);
		holder.txtPerson.getPaint().setFakeBoldText(true);
		holder.txtTotal.setText(" (" + smsInbox.total.toString() + ")");
		holder.txtSummary.setText(StringUtils.cutString(smsInbox.summary.toString(), 14));
		displayDate.setTime(smsInbox.time);
		holder.txtTime.setText(DateUtils.displayTime(now,displayDate));
		holder.smsInboxVO = smsInbox;
		convertView.setTag(holder);
		return convertView;
	}
	
}
